import org.apache.commons.cli.*;

public class CommandLineParserExample {

    public static void main(String[] args) {
        // 创建命令行选项
        Options options = new Options();
        options.addOption("init", false, "Initialize config file");
        options.addOption(Option.builder().longOpt("java_home").hasArg().desc("the JAVA_HOME location").build());
        options.addOption(Option.builder().longOpt("originalpath").hasArg().desc("the jdk download index file url.").build());

        // 创建命令行解析器
        CommandLineParser parser = new DefaultParser();

        try {
            // 解析命令行参数
            CommandLine cmd = parser.parse(options, args);

            // 处理解析后的参数
            if (cmd.hasOption("init")) {
                System.out.println("Initializing config file");
            }

            String javaHome = cmd.getOptionValue("java_home");
            if (javaHome != null) {
                System.out.println("Setting JAVA_HOME to: " + javaHome);
            }

            String originalPath = cmd.getOptionValue("originalpath");
            if (originalPath != null) {
                System.out.println("Setting jdk download index file url to: " + originalPath);
            }

        } catch (ParseException e) {
            // 处理解析异常
            System.err.println("Error parsing command line: " + e.getMessage());
            // 显示帮助信息
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("YourProgramName", options);
        }
    }
}
